#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPIP.
#    COOPIP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPIP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPIP.  If not, see <http://www.gnu.org/licenses/>.

import os
import datetime
import shutil

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopip/djangoproject/include', '/opt/coopip/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime
#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopip/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
from django.core.management import setup_environ
import djangoproject
from djangoproject import settings
setup_environ(settings)
from models import *
#********************Setup psycopg2 and django section********************

from django.template import Context, loader
from django.http import HttpResponse
from django import forms
import os
import datetime
import djangoproject.settings
import COOPIP_Misc
import djangoproject.models.ipbase
import COOPIP_Form



def BuildRecentMeterReadsForm():
    Dict={}
    Dict['PreviousDayIndicator']=forms.BooleanField(required=False)
    Dict['StartDate']=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'15'}))
    Dict['EndDate']=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'15'}))
    return Dict

def ProcessRecentMeterReadsForm(form):
    PreviousDayIndicator=COOPIP_Form.FormGetBoolean(form, 'PreviousDayIndicator')
    StartDate=COOPIP_Form.FormGetDateTime(form, 'StartDate')
    EndDate=COOPIP_Form.FormGetDateTime(form, 'EndDate')
    if PreviousDayIndicator:
        EndDate = datetime.datetime.now().date()
        StartDate = EndDate - datetime.timedelta(hours=24)
        return (StartDate, EndDate)
    elif StartDate and EndDate:
        return (StartDate, EndDate)
    return (None,None)

def PopulateRecentMeterReadsForm(form):
    form.setData({'PreviousDayIndicator' : False})
    form.data['StartDate'] = ''
    form.data['EndDate'] = ''

    return form
